import { JPush } from '@jiguang-ionic/jpush';
import { Component, ViewChild } from "@angular/core";
import { Platform, AlertController } from "ionic-angular";
import { StatusBar } from "@ionic-native/status-bar";
import { SplashScreen } from "@ionic-native/splash-screen";
import { GlobalVars } from "../providers/globalVars";
import { SQLite } from "@ionic-native/sqlite";
import { Camera } from "@ionic-native/camera";
import { PhotoViewer } from "@ionic-native/photo-viewer";
import { FileTransfer } from "@ionic-native/file-transfer";
import { File } from "@ionic-native/file";
import { DatePicker } from "@ionic-native/date-picker";
import { FileChooser } from "@ionic-native/file-chooser";
import { ThemeableBrowser } from "@ionic-native/themeable-browser";
import { LoginPage } from "../pages/login/login";
import { HttpService } from "./../providers/http.service";
import { CommonFunc } from "./../providers/commonFunc";
import { Nav } from "ionic-angular";
import { Network } from "@ionic-native/network";
import { QRScanner } from "@ionic-native/qr-scanner";
import { Geolocation } from "@ionic-native/geolocation";
import { Storage } from "@ionic/storage";
import { SetPage } from "../pages/set/set";
import { appConfig } from "../providers/config";
import { MediaCapture } from "@ionic-native/media-capture";
import { HomePage } from "../pages/home/home";
import { ChangePwdPage } from "../pages/changePwd/changePwd";
import { ChangePhonePage } from "../pages/changePhone/changePhone";
import { BackgroundMode, BackgroundModeConfiguration } from '@ionic-native/background-mode';
import { AppVersion } from '@ionic-native/app-version';
import { FileOpener } from '@ionic-native/file-opener';
import { FaceUpdatePage } from '../pages/user-face/face-update';
import { MeatasisPage } from "../pages/user-meatasis/user-meatasis";
@Component({
    templateUrl: "app.html",
    providers: [
        FileChooser,
        SQLite,
        Camera,
        PhotoViewer,
        FileTransfer,
        File,
        DatePicker,
        ThemeableBrowser,
        GlobalVars,
        HttpService,
        CommonFunc,
        Nav,
        Network,
        QRScanner,
        Geolocation,
        MediaCapture,
        BackgroundMode,
        AppVersion,
        FileOpener,
        JPush
    ]
})
export class MyApp {
    rootPage: any;
    private platformName: string = "";

    pages: Array<{ title: string, component: any }>;
    @ViewChild(Nav) nav: Nav;

    constructor(
        platform: Platform,  // 检测当前平台
        statusBar: StatusBar,  // 状态栏
        splashScreen: SplashScreen,
        private alertCtrl: AlertController,
        private storage: Storage,
        private backgroundMode: BackgroundMode
    ) {

        this.pages = [
            { title: '修改密码', component: ChangePwdPage },
            { title: '修改手机号', component: ChangePhonePage },
            /*{ title: '人脸设置', component: FaceUpdatePage },*/
            { title: '用户转移', component: MeatasisPage },
            { title: '退出登录', component: null }
        ];

        platform.ready().then(() => {
            statusBar.styleDefault();
            splashScreen.hide();
            if (platform.is("ios")) {
                this.platformName = "0";
            } else if (platform.is("android")) {
                this.platformName = "2";
                let options: BackgroundModeConfiguration = {
                    title: '温馨提醒',
                    text: '正在后台运行'
                }
                this.backgroundMode.setDefaults(options)
                this.backgroundMode.enable();
            }
            try {
                if ((<any>window).plugins == undefined) {
                    setTimeout(function () {
                        if (this.platformName == "0") {
                            (<any>window).JPush.startJPushSDK();
                        }
                        (<any>window).JPush.init();
                        window.document.addEventListener(
                            "jpush.openNotification",
                            event => {
                                if (this.platformName == "0") {
                                    let alertContent = (<any>event).aps.alert;
                                    let title = (<any>event).aps.title;

                                    let alert = this.alertCtrl.create({
                                        title: title,
                                        subTitle: alertContent,
                                        buttons: ["关闭"]
                                    });
                                    alert.present();
                                } else {
                                    this.read(event);
                                }
                            },
                            false
                        );
                    }, 2000);
                } else {
                    if (this.platformName == "0") {
                        (<any>window).JPush.startJPushSDK();
                    }
                    (<any>window).JPush.init();
                    window.document.addEventListener(
                        "jpush.openNotification",
                        event => {
                            if (this.platformName == "0") {
                                let alertContent = (<any>event).aps.alert;
                                let title = (<any>event).aps.title;

                                let alert = this.alertCtrl.create({
                                    title: title,
                                    subTitle: alertContent,
                                    buttons: ["关闭"]
                                });
                                alert.present();
                            } else {
                                this.read(event);
                            }
                        },
                        false
                    );
                }
            } catch (e) {
                //alert("111" + e);
            }
        });

        //判断是否设置过服务器地址
        this.storage.get("ip").then(ip => {
            if (!ip) {
                this.rootPage = SetPage;
            } else {
                this.storage.get("port").then(port => {
                    if (ip == 'www.hyjckj.net') {
                        appConfig.IP_URL = "https://" + ip + ":" + port + "/";
                        appConfig.PREVIEW_URL = "http://" + ip + ":8080/upload/";
                    } else if (ip == 'localhost' || ip == '127.0.0.1' || ip == '192.168.43.155') {
                        appConfig.IP_URL = "http://localhost:8099/";
                        /*appConfig.PREVIEW_URL = "http://www.hyjckj.net:8668/upload/";*/
                        appConfig.PREVIEW_URL = "http://www.hyjckj.net:8080/upload/";
                        appConfig.APK_DOWNLOAD = 'http://yhtscjg.hyjckj.net:8668/upload/app/elevator.apk';
                    } else if (ip == 'wzscjg.hyjckj.net') {
                        appConfig.IP_URL = "https://" + ip + ":" + port + "/";
                        appConfig.PREVIEW_URL = "http://" + ip + ":8668/upload/";
                        appConfig.APK_DOWNLOAD = 'http://yhtscjg.hyjckj.net:8668/upload/app/wzelevator.apk';
                    } else if (ip == 'dtwzh.szqts.gov.cn') {
                        // 市局地址
                        appConfig.IP_URL = "https://" + ip + ":" + port + "/";
                        appConfig.PREVIEW_URL = "http://58.210.227.48:8066/upload/";
                        appConfig.APK_DOWNLOAD = 'http://58.210.227.48:8066/upload/app/elevator.apk';
                    } else {
                        appConfig.IP_URL = "https://" + ip + ":" + port + "/";
                        // appConfig.IP_URL = "http://172.20.10.4:8099/";
                        appConfig.PREVIEW_URL = "http://" + ip + ":8668/upload/";
                    }
                    this.rootPage = LoginPage;

                });
            }
        });
    }
    //回调函数，一直等到 cordova加载起来
    read(event) {
        if ((<any>window).plugins == undefined) {
            setTimeout(function () {
                this.read(event);
            }, 1000);
        } else {
            let content = event.alert;
            let title = event.title;

            let alert = this.alertCtrl.create({
                title: title,
                subTitle: content,
                buttons: ["关闭"]
            });
            alert.present();
        }
    }

    openPage(page) {
        // Reset the content nav to have just this page
        // we wouldn't want the back button to show in this scenario
        console.log(page, "app页面查看");
        if (page.title == '退出登录') {
            this.nav.setRoot(LoginPage);
            return;
        } else if (page.title == '用户转移') {
            this.storage.get('user').then(val => {

                if (val.roleName == '使用系统管理员'
                    || val.roleName == '维保系统管理员'
                    || val.roleName == '监管系统管理员'
                    || val.roleName == '检验系统管理员') {
                    const alert1 = this.alertCtrl.create({
                        // title: '',
                        subTitle: "系统管理员账号不能被转移",
                        buttons: ["确定"]
                    });
                    alert1.present();
                    return;
                } else {
                    this.nav.push(page.component);
                }
            });
        } else {
            this.nav.push(page.component);
        }

    }
}